home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14469 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: solon.com!not-for-mail
  2. From: gohel@csee.usf.edu (Himanshu Gohel)
  3. Newsgroups: comp.lang.c.moderated,comp.lang.c
  4. Subject: fflush(stdin) - not guaranteed to work?
  5. Followup-To: comp.lang.c.moderated,comp.lang.c
  6. Date: 14 Apr 1996 23:37:11 -0500
  7. Organization: Geometric Modeling and Computer Graphics Group, USF Tampa, FL.
  8. Sender: clc@solutions.solon.com
  9. Approved: clc@solutions.solon.com
  10. Message-ID: <4ksjpn$rjt@solutions.solon.com>
  11. Reply-To: gohel@csee.usf.edu
  12. NNTP-Posting-Host: solutions.solon.com
  13.  
  14. In Paul S. Wang's "Introduction to ANSI C on UNIX" he says that
  15. the fflush() function was "not intended to control input buffering"
  16. on page 266.
  17.  
  18. My question is, why?  Some of my students have written programs using
  19. fflush(stdin) hoping it would clear away anything that's in the input
  20. buffer before a scanf() statement and apparently it works on some PC
  21. based compilers, but when re-compiled on a UNIX system, it does not
  22. always work.
  23.  
  24. What is the best way to get rid of the '\n' from the input buffer after
  25. a scanf() statement?  In the same book on page 263 I've seen the following
  26. format used in an fscanf() format specifier:
  27.  
  28.  %*[\n]
  29.  
  30. which is explained as follows:
  31.  
  32. "...and %*[\n] consumes and discards the NEWLINE character at the end of
  33.  an input line."
  34.  
  35. But I've not had much luck with it when used as follows:
  36.  
  37.     scanf("%c %*[\n]", &test);
  38.  
  39. The NEWLINE stays in the buffer and thus interferes with the next scanf()
  40. statement. I am aware, however, of the " "%c" format which discards white
  41. space before a character input, but my question is a more general one to
  42. discard the NEWLINE.
  43.  
  44. If you do post to the newsgroup, please do send me a copy via e-mail too.
  45. Thanks!
  46.  
  47. -- 
  48. Himanshu Gohel, gohel@csee.usf.edu         | You only have to do very few
  49. WEB URL: http://www.csee.usf.edu/~gohel/     | things right in your life as
  50. Geometric Modeling & Graphics Research Group | long as you don't do too many
  51. U of South Florida, Tampa, FL. USA.         | things wrong - Warren Buffett
  52.